//
//  CHMaskView.swift
//  nurseApp
//
//  Created by cyptt on 2022/8/11.
//

import UIKit
/*
 图片的位置
 */
public enum CH_MASK_LOCATION_TYPE : Int {
    
    case center = 0   //中间
    case top = 1  //上面
    case bottom = 2  //下面
}
open class CHMaskView: UIView {

  
  
    let myWindow:UIWindow? = UIApplication.shared.keyWindow
    public override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUI()
    }
    
    public  required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    open  override func awakeFromNib() {
        super.awakeFromNib()
        self.setUI()
    }

    
    func setUI(){
   
    }
    
    
    public override func removeFromSuperview() {
        super.removeFromSuperview()
        self.mskView.removeFromSuperview()
      
    }
    
    /*
     显示
     @param fatherV 价值到俯视图上
     */
    open func show(fatherV:UIView){
        fatherV.addSubview(self.mskView)
        fatherV.addSubview(self)
        self.center = self.mskView.center
    }
    
    /*
     显示位置
     */
    open  func show(fatherV:UIView,location:CH_MASK_LOCATION_TYPE,isSafeView:Bool = false){
       
        fatherV.addSubview(self.mskView)
        fatherV.addSubview(self)
        if location == .center{
            
            self.center.x = self.mskView.center.x
            self.center.y = self.mskView.center.y
        }else if(location == .top){
            
            self.center.x = self.mskView.center.x
            if #available(iOS 11.0, *) {
                self.center.y = self.frame.size.height/2 + self.mskView.safeAreaInsets.top
            } else {
                self.center.y = self.frame.size.height/2
            }
        }else if location == .bottom{
            fatherV.addSubview(self.mskView)
            fatherV.addSubview(self)
            self.center.x = self.mskView.center.x
           
            if #available(iOS 11.0, *) {
                
          
                
                if isSafeView == false{
                    self.center.y = self.mskView.frame.size.height - self.frame.size.height/2 -    self.mskView.safeAreaInsets.bottom
                }else{
                    self.center.y = self.mskView.frame.size.height - self.frame.size.height/2
                }

            } else {
                self.center.y = self.mskView.frame.size.height - self.frame.size.height/2
            
            }
        }
       
    }
    /*
     显示位置和间距
     */
     open  func show(fatherV:UIView,location:CH_MASK_LOCATION_TYPE,margin:CGFloat,isSafeView:Bool = false){
        fatherV.addSubview(self.mskView)
        fatherV.addSubview(self)
        if location == .center{
         
            self.center.x = self.mskView.center.x
            self.center.y = self.mskView.center.y + margin
        }else if(location == .top){
        
            self.center.x = self.mskView.center.x
            if #available(iOS 11.0, *) {
                self.center.y = self.frame.size.height/2 + self.mskView.safeAreaInsets.top + margin
            } else {
                self.center.y = self.frame.size.height/2 + margin
            }
        }else if location == .bottom{
      
            self.center.x = self.mskView.center.x
           
            if #available(iOS 11.0, *) {
                
                if isSafeView == false{
                    self.center.y = self.mskView.frame.size.height - self.frame.size.height/2 -  self.mskView.safeAreaInsets.bottom  - margin
                }else{
                    self.center.y = self.mskView.frame.size.height - self.frame.size.height/2   - margin
                }
           

            } else {
                self.center.y = self.mskView.frame.size.height - self.frame.size.height/2 - margin

            }
        }
       
    }
    /*
     默认中间
     */
    open  func show(){
        myWindow?.addSubview(self.mskView)
        myWindow?.addSubview(self)
        self.center = self.mskView.center
    }
    
    /*
     显示位置
     @param isSafeView 是否占据上下安全区域 false 不占据 true 占据
     */
    open  func show(location:CH_MASK_LOCATION_TYPE,isSafeView:Bool = false){
       
        myWindow?.addSubview(self.mskView)
        myWindow?.addSubview(self)
        if location == .center{
            
            self.center.x = self.mskView.center.x
            self.center.y = self.mskView.center.y
        }else if(location == .top){
            
            self.center.x = self.mskView.center.x
            if #available(iOS 11.0, *) {
                self.center.y = self.frame.size.height/2 + self.mskView.safeAreaInsets.top
            } else {
                self.center.y = self.frame.size.height/2
            }
        }else if location == .bottom{
            myWindow?.addSubview(self.mskView)
            myWindow?.addSubview(self)
            self.center.x = self.mskView.center.x
           
            if #available(iOS 11.0, *) {
                
                if isSafeView == false{
                    self.center.y = self.mskView.frame.size.height - self.frame.size.height/2 -    self.mskView.safeAreaInsets.bottom
                }else{
                    self.center.y = self.mskView.frame.size.height - self.frame.size.height/2
                }
       

            } else {
                self.center.y = self.mskView.frame.size.height - self.frame.size.height/2
            
            }
        }
       
    }
    
    /*
     显示位置和间距
     */
    open  func show(location:CH_MASK_LOCATION_TYPE,margin:CGFloat,isSafeView:Bool = false){
        myWindow?.addSubview(self.mskView)
        myWindow?.addSubview(self)
        if location == .center{
         
            self.center.x = self.mskView.center.x
            self.center.y = self.mskView.center.y + margin
        }else if(location == .top){
        
            self.center.x = self.mskView.center.x
            if #available(iOS 11.0, *) {
                self.center.y = self.frame.size.height/2 + self.mskView.safeAreaInsets.top + margin
            } else {
                self.center.y = self.frame.size.height/2 + margin
            }
        }else if location == .bottom{
      
            self.center.x = self.mskView.center.x
           
            if #available(iOS 11.0, *) {
                
                if isSafeView == false{
                    self.center.y = self.mskView.frame.size.height - self.frame.size.height/2 -  self.mskView.safeAreaInsets.bottom  - margin
                }else{
                    self.center.y = self.mskView.frame.size.height - self.frame.size.height/2  - margin
                }
             

            } else {
                self.center.y = self.mskView.frame.size.height - self.frame.size.height/2 - margin

            }
        }
       
    }
    lazy var mskView = {()-> UIView in
        let view:UIView = UIView.init(frame: CGRect.init(x: 0, y: 0, width:myWindow!.frame.size.width , height: myWindow!.frame.size.height))
        view.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        return view
    }()
    
}


